#
# Copyright (c) nexB Inc. and others. All rights reserved.
# ScanCode is a trademark of nexB Inc.
# SPDX-License-Identifier: Apache-2.0
# See http://www.apache.org/licenses/LICENSE-2.0 for the license text.
# See https://github.com/nexB/scancode-toolkit for support or download.
# See https://aboutcode.org for more information about nexB OSS projects.
#

import os

from commoncode.testcase import FileBasedTesting

from scancode.cli_test_utils import check_json_scan
from scancode.cli_test_utils import run_scan_click
from scancode_config import REGEN_TEST_FIXTURES
from summarycode import generated


class TestGeneratedCode(FileBasedTesting):
    test_data_dir = os.path.join(os.path.dirname(__file__), 'data')

    def test_basic(self):
        expected = [
            '// This file was generated by the JavaTM Architecture '.lower() +
            'for XML Binding(JAXB) Reference Implementation'.lower(),
            '// Generated on: 2011.08.01 at 11:35:59 AM CEST'.lower()
        ]
        test_file = self.get_test_loc('generated/simple/generated_1.java')
        result = list(generated.get_generated_code_hint(location=test_file))
        assert result == expected

    def test_basic_alt(self):
        expected = [
            '// This file was generated by the JavaTM Architecture '.lower() +
            'for XML Binding(JAXB) Reference Implementation'.lower(),
            '// Generated on: 2013.11.15 at 04:17:00 PM CET'.lower()
        ]
        test_file = self.get_test_loc('generated/simple/generated_3.java')
        result = list(generated.get_generated_code_hint(location=test_file))
        assert result == expected

    def test_basic2(self):
        expected = ['* This class was generated by the JAX-WS RI.'.lower()]
        test_file = self.get_test_loc('generated/simple/generated_2.java')
        result = list(generated.get_generated_code_hint(location=test_file))
        assert result == expected

    def test_basic3(self):
        expected = ['/* This class was automatically generated'.lower()]
        test_file = self.get_test_loc('generated/simple/generated_4.java')
        result = list(generated.get_generated_code_hint(location=test_file))
        assert result == expected

    def test_basic4(self):
        expected = [
            '* <p>The following schema fragment specifies the '.lower() +
            'expected content contained within this class.'.lower()
        ]
        test_file = self.get_test_loc('generated/simple/generated_5.java')
        result = list(generated.get_generated_code_hint(location=test_file))
        assert result == expected

    def test_basic5(self):
        expected = ['/* DO NOT EDIT THIS FILE - it is machine generated */'.lower()]
        test_file = self.get_test_loc('generated/simple/generated_6.c')
        result = list(generated.get_generated_code_hint(location=test_file))
        assert result == expected

    def test_configure(self):
        expected = [
            '# Generated by GNU Autoconf 2.64 for Apache CouchDB 1.0.1.'.lower()
        ]
        test_file = self.get_test_loc('generated/simple/configure')
        result = list(generated.get_generated_code_hint(location=test_file))
        assert result == expected

    def test_tomcat_jspc(self):
        expected = [
            '<!--Automatically created by Apache Jakarta Tomcat JspC.'.lower()
        ]
        test_file = self.get_test_loc('generated/jspc/web.xml')
        result = list(generated.get_generated_code_hint(location=test_file))
        assert result == expected

    def test_generated_cli_option(self):
        test_dir = self.get_test_loc('generated/simple')
        result_file = self.get_temp_file('json')
        expected_file = self.get_test_loc('generated/cli.expected.json')
        run_scan_click(['--generated', '--json-pp', result_file, test_dir])
        check_json_scan(expected_file, result_file, remove_file_date=True, regen=REGEN_TEST_FIXTURES)

